<?php

namespace backend\controllers;

use Yii;
use backend\models\MenuType;
use backend\models\search\MenuTypeSearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
 * MenuTypeController implements the CRUD actions for MenuType model.
 */
class MenuTypeController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors(){
        return ArrayHelper::merge(parent::behaviors(),[
            
        ]);
    }

    /**
     * Lists all MenuType models.
     * @return mixed
     */
    public function actionIndex()
    {    
        $searchModel = new MenuTypeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    /**
     * Displays a single MenuType model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {   
        $request = Yii::$app->request;
        Yii::$app->response->format = Response::FORMAT_JSON;
        return [
                'title'=> Yii::t('common', 'View'),
                'content'=>$this->renderAjax('view', [
                    'model' => $this->findModel($id),
                ]),
                'footer'=> Html::button(Yii::t('common', 'Close'),['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                        Html::a(Yii::t('common', 'Edit'),['update','id'=>$id],['class'=>'btn btn-primary','role'=>'modal-remote'])
            ];    
    }

    /**
     * Creates a new MenuType model.
     * For ajax request will return json object
     * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;
        $model = new MenuType();  

        Yii::$app->response->format = Response::FORMAT_JSON;
        if($request->isPost && $model->load($request->post()) && $model->save()){
            return [
                'forceReload'=>'#crud-datatable-pjax',
                'title'=> Yii::t('common', 'Create'),
                'content'=>'<span class="text-success">'.Yii::t('common', 'Create success').'</span>',
                'footer'=> Html::button(Yii::t('common', 'Close'),['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                        Html::a(Yii::t('common', 'Create More'),['create'],['class'=>'btn btn-primary','role'=>'modal-remote'])

            ];         
        }
        return [
            'title'=> Yii::t('common', 'Create'),
            'content'=>$this->renderAjax('create', [
                'model' => $model,
            ]),
            'footer'=> Html::button(Yii::t('common', 'Close'),['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                        Html::button(Yii::t('common', 'Save'),['class'=>'btn btn-primary','type'=>"submit"])

        ];          
       
    }

    /**
     * Updates an existing MenuType model.
     * For ajax request will return json object
     * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $request = Yii::$app->request;
        $model = $this->findModel($id);       

        Yii::$app->response->format = Response::FORMAT_JSON;
        if($request->isPost && $model->load($request->post()) && $model->save()){
            return [
                'forceReload'=>'#crud-datatable-pjax',
                'title'=> Yii::t('common', 'Update'),
                'content'=>$this->renderAjax('view', [
                    'model' => $model,
                ]),
                'footer'=> Html::button(Yii::t('common', 'Close'),['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                        Html::a(Yii::t('common', 'Edit'),['update','id'=>$id],['class'=>'btn btn-primary','role'=>'modal-remote'])
            ];    
        }
        return [
            'title'=> Yii::t('common', 'Update'),
            'content'=>$this->renderAjax('update', [
                'model' => $model,
            ]),
            'footer'=> Html::button(Yii::t('common', 'Close'),['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                        Html::button(Yii::t('common', 'Save'),['class'=>'btn btn-primary','type'=>"submit"])
        ];           
    }

    /**
     * Delete an existing MenuType model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $request = Yii::$app->request;
        $this->findModel($id)->delete();

        Yii::$app->response->format = Response::FORMAT_JSON;
        return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];


    }

     /**
     * Delete multiple existing MenuType model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionBulkDelete()
    {        
        $request = Yii::$app->request;
        $pks = explode(',', $request->post( 'pks' )); // Array or selected records primary keys
        foreach ( $pks as $pk ) {
            $model = $this->findModel($pk);
            $model->delete();
        }

        Yii::$app->response->format = Response::FORMAT_JSON;
        return ['forceClose'=>true,'forceReload'=>'#crud-datatable-pjax'];
       
    }

    /**
     * Finds the MenuType model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return MenuType the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = MenuType::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
